package com.control.qct.businesssteward.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.base.BaseActivity;
import com.control.qct.businesssteward.base.BaseBean;
import com.control.qct.businesssteward.entity.TabEntity;
import com.control.qct.businesssteward.fragment.HomeFragment;
import com.control.qct.businesssteward.fragment.MySelfFragment;
import com.control.qct.businesssteward.fragment.NewsFragment;
import com.control.qct.businesssteward.httputils.HttpUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class HomeActivity extends BaseActivity implements OnTabSelectListener {

    @BindView(R.id.ctl_bottom)
    CommonTabLayout mCtlBottom;

    //第一次点击返回键的时间
    private long firstPressedTime;
    private String[] mTitles = {"首页", "消息", "我的"}; //标题
    private int[] mIconUnselectIds = {R.drawable.icon_shouye_shouye_n, R.drawable.icon_shouye_xiaoxi_n, R.drawable
            .icon_shouye_wode_n}; //未选中图标
    private int[] mIconSelectIds = {R.drawable.icon_shouye_shouye_s, R.drawable.icon_shouye_xiaoxi_s, R.drawable
            .icon_shouye_wode_s}; //选中图标
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>(); //布局参数集合
    private ArrayList<Fragment> mFragments = new ArrayList<>();//fragment集合
    private String mUsername;

    public HomeActivity() {
        super(R.layout.activity_home);
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        mUsername = extras.getString("username");
    }

    @Override
    protected void initView() {
        setBottomTab();
    }

    /**
     * 设置底部导航栏
     */
    private void setBottomTab() {
        //添加标题 已选图标 未选图标
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        //添加fragment
        mFragments.add(new HomeFragment());
        mFragments.add(new NewsFragment());
        mFragments.add(new MySelfFragment(mUsername));
        /**懒人模式**/
        mCtlBottom.setTabData(mTabEntities, this, R.id.content, mFragments);
        /** 切换状态**/
        mCtlBottom.setOnTabSelectListener(this);
    }

    @Override
    public void onTabSelect(int position) {
        switch (position) {
            case 0:  //首页
                break;
            case 1:  //消息
                break;
            case 2:  //我的
                break;
        }
    }

    @Override
    public void onTabReselect(int position) { //重复点击

    }

    @Override
    public void onBackPressed() {
        //System.currentTimeMillis() 当前系统时间即第二次点击返回键的时间
        if (System.currentTimeMillis() - firstPressedTime < 2000) {
            super.onBackPressed();
        } else {
            showShortToast("再点一次退出");
            firstPressedTime = System.currentTimeMillis();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //软件退出后 清除token跟userCode
        mSPUtils.remove("token");
        mSPUtils.remove("user_code");
    }
}
